package com.toman.Adpter;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.toman.Entity.Administer_Entity;
import com.toman.MyApplication;
import com.toman.R;

import java.util.ArrayList;

import com.squareup.picasso.Picasso;

/**
 * Created by  GridView_adpter on Administrator on 2017/11/3 on 16:13.
 */

public class Administer_adpter extends BaseAdapter {

    //上下文对象
    Context context;
    //
    ArrayList<Administer_Entity> arrayList;

    LayoutInflater inflater;

    MyApplication myApplication;

    LinearLayout mAd_bg;
    ImageView mdel_img;


    public Administer_adpter(Context context, ArrayList<Administer_Entity> arrayList, MyApplication myApplication) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;
        //     if (null == convertView){
        addPackage = new AddPackage();
        convertView = inflater.inflate(R.layout.administer_item, null, false);
        addPackage.img = (ImageView) convertView.findViewById(R.id.mAdminImg);
        addPackage.title = (TextView) convertView.findViewById(R.id.mAdminTitle);
        mAd_bg = (LinearLayout) convertView.findViewById(R.id.mAdmin_bg);
        mdel_img = (ImageView) convertView.findViewById(R.id.mAdmin_img);

//            convertView.setTag(addPackage);
//        }else {
//            addPackage=(AddPackage) convertView.getTag();
//        }

        if (myApplication.getSp().getString("token", "").equals("toman")) {
            mAd_bg.setBackgroundResource(R.color.loginbg);

        } else if (myApplication.getSp().getString("token", "").equals("operater")) {
            mAd_bg.setBackgroundResource(R.color.g7bg);

        } else if (myApplication.getSp().getString("token", "").equals("company")) {
            mAd_bg.setBackgroundResource(R.color.c1);
            //mdel_img.setVisibility(View.VISIBLE);
        }

        if (arrayList.get(position).isoks()) {
            mdel_img.setVisibility(View.VISIBLE);
        } else {
            mdel_img.setVisibility(View.GONE);
        }

        if (arrayList.get(position).getImg().equals("")) {
            Picasso.with(context).load(R.mipmap.chanxian).into(addPackage.img);
        } else {
            Picasso.with(context).load(arrayList.get(position).getImg()).into(addPackage.img);

        }

        addPackage.title.setText(arrayList.get(position).getTitle());

        mdel_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DelDialog(position);

            }
        });


        return convertView;
    }


    public class AddPackage {
        ImageView img;
        TextView title;
    }


    /*
        提示删除
     */
    public void DelDialog(final Integer position) {
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.del_layout1);

        dialog.getWindow().findViewById(R.id.mDetermine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

    }


}
